package niuke;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 1.
 *
 * @author yangxiaogen
 * @date 2023/3/7 20:10
 */
public class HJ04 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { // 注意 while 处理多个 case
            // String inputStr = in.nextLine();
            String inputStr = "abc";
            int len = 8;
            List<String> splitLengthString = splitLengthString(inputStr, len);
            splitLengthString.forEach(System.out::println);
        }
    }



    private static List<String> splitLengthString(String original, int length) {
        String tmp = original;
        List<String> response = new ArrayList<>();
        if (tmp == null) {
            return response;
        }
        char[] requestChar = tmp.toCharArray();
        int originalLength = requestChar.length;
        int isLength = originalLength % length;
        if (isLength != 0) {
            int repairZeroNum = length - isLength;
            StringBuilder stringBuilder = new StringBuilder(tmp);
            for (int i = 0; i < repairZeroNum; i++) {
                stringBuilder.append("0");
            }
            tmp = stringBuilder.toString();
        }
        // response = getListStr(tmp,length);
        response = getListStrMy(tmp, length);
        return response;
    }

    public static List<String> getListStrMy(String str, int len) {
        List<String> response = new ArrayList<>();
        char[] chars = str.toCharArray();
        StringBuilder strBuilder = new StringBuilder(len);
        for (int i = 0; i < chars.length; i++) {
            strBuilder.append(chars[i]);
            if (((i + 1) % len) == 0) {
                response.add(strBuilder.toString());
                strBuilder = new StringBuilder(len);
            }
        }
        return response;
    }

    /**
     * 把String按一定长度拆分，返回ListString
     */
    public static List<String> getListStr(String str, int len) {
        List<String> listStr = new ArrayList<>();
        int strLen = str.length();
        int start = 0;
        int num = len;
        String temp = null;
        while (true) {
            try {
                if (num >= strLen) {
                    temp = str.substring(start, strLen);
                } else {
                    temp = str.substring(start, num);
                }
            } catch (Exception e) {
                break;
            }
            listStr.add(temp);
            start = num;
            num = num + len;
        }
        return listStr;
    }

    @Test
    public void test3() {
        String inputStr = "12345678876543213";
        int len = 8;
        List<String> splitLengthString = splitLengthString(inputStr, len);
        splitLengthString.forEach(System.out::println);
    }

    @Test
    public void test() {
        String s = "1234567887654321";
        for (int i = 0; i < 2; i++) {
            System.out.println(s.substring(0, 8));
        }
    }

    @Test
    public void test2() {
        int len = 8;
        for (int i = 0; i < 17; i++) {
            System.out.println(i % len);
        }
    }

    private int cou;

    HJ04(int cou) {
        cou = cou;
    }
    @Test
    public void test44() {
        System.out.println(new HJ04(99).cou);
    }


}
